/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.PaginationUtil;
import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.BuildingListingForm;
import com.ntb.entities.Buildings;
import com.ntb.models.BuildingStatusDAO;
import com.ntb.models.BuildingTypesDAO;
import com.ntb.models.BuildingsDAO;
import com.ntb.models.impl.BuildingStatusDAOImpl;
import com.ntb.models.impl.BuildingTypesDAOImpl;
import com.ntb.models.impl.BuildingsDAOImpl;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class BuildingListingAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

        BuildingListingForm buildingListingForm = (BuildingListingForm) form;

        BuildingsDAO buildingDAO = new BuildingsDAOImpl();

        String action = buildingListingForm.getAction();

        List<String> errorMessages = new ArrayList();
        List<String> successMessages = new ArrayList();
        boolean isHaveAction = true;
        if (("delete").equalsIgnoreCase(action)) {
            if (buildingDAO.delete(buildingListingForm.getBuildingID()) == 0) {
                errorMessages.add("There was an error deleting this building. You must delete all buildings associated with this building before deleting the building.");
            } else {
                buildingDAO.delete(buildingListingForm.getBuildingID());
                successMessages.add("1 buildings out of 1 successfully deleted.");
            }
        } else if (("deleteBuildings").equalsIgnoreCase(action)) {
            int[] buildingIDs = buildingListingForm.getBuildingIDs();
            int count = 0;

            int length = 0;

            try {
                length = buildingIDs.length;
            } catch (Exception ex) {
            }

            if (length > 0) {
                for (int buildingID : buildingIDs) {
                    if (buildingDAO.delete(buildingID) == 0) {
                        errorMessages.add("There was an error deleting this building. You must delete all buildings associated with this building before deleting the building.");
                    } else {
                        buildingDAO.delete(buildingID);
                        count++;
                    }
                }
                successMessages.add(count + " buildings out of " + length + " successfully deleted.");
            }
        } else {
            isHaveAction = false;
        }

        if (isHaveAction) {
            request.setAttribute("errorMessages", errorMessages);
            request.setAttribute("successMessages", successMessages);
        }

        BuildingTypesDAO typeDao = new BuildingTypesDAOImpl();
        request.setAttribute("buildingTypeList", typeDao.getList());

        BuildingStatusDAO statusDao = new BuildingStatusDAOImpl();
        request.setAttribute("statusList", statusDao.getStatusTable());

        String baseUrl = request.getRequestURI() + "?";

        String buildingId = null;
        String buildingName = null;
        String landId = null;
        int buildingTypeId = -1;
        int floorFrom = -1;
        int floorTo = -1;
        int roomFrom = -1;
        int roomTo = -1;
        String constructedDateFrom = null;
        String constructedDateTo = null;
        String status = null;

        int page = -1;

        buildingId = buildingListingForm.getId();
        buildingId = "".equals(buildingId) ? null : buildingId;
        if (buildingId != null) {
            baseUrl += "id=" + buildingId + "&amp;";
        }

        buildingName = buildingListingForm.getName();
        buildingName = "".equals(buildingName) ? null : buildingName;
        if (buildingName != null) {
            baseUrl += "name=" + buildingName + "&amp;";
        }

        landId = buildingListingForm.getLand();
        landId = "".equals(landId) ? null : landId;
        if (landId != null) {
            baseUrl += "land=" + landId + "&amp;";
        }

        buildingTypeId = buildingListingForm.getType();
        if (buildingTypeId > 0) {
            baseUrl += "type=" + buildingTypeId + "&amp;";
        } else {
            buildingTypeId = -1;
        }

        try {
            floorFrom = Integer.parseInt(buildingListingForm.getFloorF());
        } catch (Exception e) {
        }
        if (floorFrom > 0) {
            baseUrl += "floorF=" + floorFrom + "&amp;";
        } else {
            floorFrom = -1;
        }

        try {
            floorTo = Integer.parseInt(buildingListingForm.getFloorT());
        } catch (Exception e) {
        }
        if (floorTo > 0) {
            baseUrl += "floorT=" + floorTo + "&amp;";
        } else {
            floorTo = -1;
        }

        try {
            roomFrom = Integer.parseInt(buildingListingForm.getRoomF());
        } catch (Exception e) {
        }
        if (roomFrom > 0) {
            baseUrl += "roomF=" + roomFrom + "&amp;";
        } else {
            roomFrom = -1;
        }

        try {
            roomTo = Integer.parseInt(buildingListingForm.getRoomT());
        } catch (Exception e) {
        }
        if (roomTo > 0) {
            baseUrl += "roomT=" + roomTo + "&amp;";
        } else {
            roomTo = -1;
        }


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            df.parse(buildingListingForm.getDateF());
            constructedDateFrom = buildingListingForm.getDateF();
        } catch (Exception ex) {
            constructedDateFrom = null;
        }
        if (constructedDateFrom != null) {
            baseUrl += "dateF=" + constructedDateFrom + "&amp;";
        }

        try {
            df.parse(buildingListingForm.getDateT());
            constructedDateTo = buildingListingForm.getDateT();
        } catch (Exception ex) {
            constructedDateTo = null;
        }
        if (constructedDateTo != null) {
            baseUrl += "dateT=" + constructedDateTo + "&amp;";
        }

        status = buildingListingForm.getStatus();
        status = "".equals(status) ? null : status;
        if (status != null) {
            baseUrl += "status=" + status + "&amp;";
        }

        try {
            int i = Integer.parseInt(request.getParameter("page"));
            page = i > 1 ? i : 1;
        } catch (Exception ex) {
        }
        System.out.println("Status" + status);

        Iterator<Buildings> buildingList = buildingDAO.getList(buildingId, buildingName, landId, buildingTypeId, floorFrom, floorTo, roomFrom, roomTo, constructedDateFrom, constructedDateTo, status, page);
        request.setAttribute("buildingList", buildingList);

        PaginationUtil paginationUtil = new PaginationUtil();
        paginationUtil.setBaseUrl(baseUrl);
        paginationUtil.setNumPages(buildingDAO.getPageCount(buildingId, buildingName, landId, buildingTypeId, floorFrom, floorTo, roomFrom, roomTo, constructedDateFrom, constructedDateTo, status));
        paginationUtil.setCurPage(page);
        request.setAttribute("pagination", paginationUtil.createLinks());

        return mapping.getInputForward();
    }
}
